package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Cart;
import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class OrderFormServlet extends HttpServlet {
    private static final String ORDER_FORM = "/WEB-INF/jsp/order/orderForm.jsp";

    private Cart cart;

    private Account account;

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        // 获得购物车
        cart = (Cart) session.getAttribute("cart");
        if (cart == null){
            cart = new Cart();
            session.setAttribute("cart",cart);
        }

        // 获得账户
       else {
            // 跳转到订单页面
            Order order = new Order();
            // 生成订单
            account = (Account) session.getAttribute("loginAccount");
            order.initOrder(account,cart);

            session.setAttribute("creditCardTypes",order.getCardType());
            session.setAttribute("order",order);


            Account account = (Account) session.getAttribute("loginAccount");
            if(account != null) {
                String strUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                        + req.getContextPath() + req.getServletPath() + "?" + req.getQueryString();


                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") +"用户" +account.getUsername()+ "  " + "跳转至订单页面";
                logService.insertLogInfo(account.getUsername(),logInfo);
            }

            req.getRequestDispatcher(ORDER_FORM).forward(req,resp);
        }
    }
}
